package demo.gui;

import java.awt.*;
import java.awt.event.*;

public class MyFrame {
	private Frame mainFrame;
	private Button exitBut;
	private Button mouseBut;
	private Button keyBut;
	private TextField tf;

	public MyFrame() {
		init();
	}

	private void init() {
		mainFrame = new Frame();
		exitBut = new Button("�ر�");
		mouseBut = new Button("���");
		keyBut = new Button("����");
		tf = new TextField(10);
		mainFrame.setBounds(300, 200, 500, 400);
		mainFrame.setLayout(new FlowLayout());
		mainFrame.add(exitBut);
		mainFrame.add(mouseBut);
		mainFrame.add(keyBut);
		mainFrame.add(tf);
		addEvent();
		mainFrame.setVisible(true);
	}

	private void addEvent() {
		mainFrame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		exitBut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		mouseBut.addMouseListener(new MouseAdapter() {
			private int count = 0;

			public void mouseEntered(MouseEvent e) {
				System.out.println("����");
			}

			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2)
					System.out.println("˫��" + ++count);
				else
					System.out.println("���" + ++count);
			}

			public void mouseExited(MouseEvent e) {
				System.out.println("�뿪");
			}

			public void mousePressed(MouseEvent e) {
				System.out.println("����");
			}

			public void mouseReleased(MouseEvent e) {
				System.out.println("�ɿ�");
			}
		});
		keyBut.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_ENTER)
					System.out.println("Ctrl+Enter");
				else if ((e.getKeyCode() == 27) || (e.getKeyCode() == KeyEvent.VK_ENTER))
					System.exit(0);
				else
					System.out.println(KeyEvent.getKeyText(e.getKeyCode()));
			}
		});
		tf.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				int code = e.getKeyCode();
				if (!((code <= KeyEvent.VK_9 && code >= KeyEvent.VK_0)
						|| (code <= KeyEvent.VK_NUMPAD9 && code >= KeyEvent.VK_NUMPAD0))) {
					e.consume();
					System.out.println(KeyEvent.getKeyText(code) + "�ǷǷ���");
				}
			}
		});
	}

	public static void main(String[] args) {
		new MyFrame();
	}
}
